package com.yxboot.modules.call.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yxboot.common.api.Result;
import com.yxboot.common.aspect.SysLogOperation;
import com.yxboot.common.pagination.PageRequest;
import com.yxboot.enums.UpdateType;
import com.yxboot.modules.call.entity.XNoQs;
import com.yxboot.modules.call.service.XNoQsService;
import com.yxboot.modules.call.vo.BatchUpdateReqVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * X号码表 Api
 * @author exchenming
 * @date 2025-08-20
 */
@RestController
@RequestMapping("/x/noQs")
@RequiredArgsConstructor
@Tag(name = "X号码表 Api")
public class XNoQsController {
    private final XNoQsService xNoQsService;

    @PostMapping("/list")
    @Operation(summary = "X号码表 查询列表接口")
    public Result list(XNoQs xNoQs,PageRequest pageRequest) {
        XNoQs xNoQs1 = xNoQs;
        IPage<XNoQs> pageResult = xNoQsService.pageQuery(xNoQs,pageRequest);
        return Result.success("查询成功！", pageResult);
    }


    @GetMapping("/get")
    @Operation(summary = "查询详情接口")
    public Result get(@RequestParam Long id) {
        XNoQs xNoQs = xNoQsService.getById(id);
        return Result.success("查询成功！", xNoQs);
    }

    @PostMapping("/save")
    @SysLogOperation(value = "保存信息接口")
    @Operation(summary = "保存信息接口")
    public Result save(@RequestBody XNoQs xNoQs) {
        xNoQsService.saveOrUpdate(xNoQs);
        return Result.success("保存成功！", xNoQs);
    }

    @DeleteMapping("/remove")
    @SysLogOperation(value = "删除信息接口")
    @Operation(summary = "删除信息接口")
    public Result remove(Long id) {
        xNoQsService.removeById(id);
        return Result.success("删除成功！");
    }

    @PostMapping("/lock")
    @SysLogOperation(value = "锁定接口")
    @Operation(summary = "锁定接口")
    public Result lock(Long id) {
        XNoQs xNoQs = xNoQsService.getById(id);
        if(Objects.isNull(xNoQs)){
            return Result.error("X号码不存在");
        }
        if("Y".equals(xNoQs.getIsLock())){
            xNoQs.setIsLock("N");
        } else {
            xNoQs.setIsLock("Y");
        }

        return save(xNoQs);
    }

    @PostMapping("/cancel")
    @SysLogOperation(value = "退订接口")
    @Operation(summary = "退订接口")
    public Result cancel(Long id) {
        XNoQs xNoQs = xNoQsService.getById(id);
        if(Objects.isNull(xNoQs)){
            return Result.error("X号码不存在");
        }
        xNoQs.setIsLock("Y");
        if("Y".equals(xNoQs.getIsCancel())){
            xNoQs.setIsCancel("N");
        } else {
            xNoQs.setIsCancel("Y");
        }
        xNoQs.setCancelTime(LocalDateTimeUtil.format(LocalDateTimeUtil.now(),"yyyy-MM-dd"));
        return save(xNoQs);
    }

    @PostMapping("/import")
    @SysLogOperation(value = "导入接口")
    @Operation(summary = "导入接口")
    public Result importExcel(@RequestPart("file") MultipartFile file) {
        xNoQsService.importExcel(file);
        return Result.success("导入成功");
    }

    @GetMapping("/getAllProvince")
    @SysLogOperation(value = "所有省份")
    @Operation(summary = "所有省份")
    public Result<List<XNoQs>> getAllProvince(){
        List<XNoQs> provinces = xNoQsService.getAllProvince();
        return Result.success("查询所有省份成功",provinces);
    }

    @GetMapping("/getCityByProvince")
    @SysLogOperation(value = "省份城市")
    @Operation(summary = "省份城市")
    public Result<List<XNoQs>> getCityByProvince(String provinceName){
        List<XNoQs> citys = xNoQsService.getCityByProvince(provinceName);
        return Result.success("查询所有城市成功",citys);

    }



    @PostMapping("/batchUse")
    @SysLogOperation(value = "批量锁定接口")
    @Operation(summary = "批量锁定接口")
    public Result batchUse(@RequestBody BatchUpdateReqVo rew) {
        boolean pass = xNoQsService.batchUpdateIds(rew.getIds(),UpdateType.USE);
        if(pass){
            return Result.success("批量使用成功");
        } else {
            return Result.error("批量使用失败");
        }
    }

    @PostMapping("/batchLock")
    @SysLogOperation(value = "批量锁定接口")
    @Operation(summary = "批量锁定接口")
    public Result batchLock(@RequestBody BatchUpdateReqVo rew) {
        boolean pass = xNoQsService.batchUpdateIds(rew.getIds(),UpdateType.LOCK);
        if(pass){
            return Result.success("批量锁定成功");
        } else {
            return Result.error("批量锁定失败");
        }
    }
    @PostMapping("/batchCancel")
    @SysLogOperation(value = "批量锁定接口")
    @Operation(summary = "批量锁定接口")
    public Result batchCancel(@RequestBody BatchUpdateReqVo rew) {
        boolean pass = xNoQsService.batchUpdateIds(rew.getIds(),UpdateType.CANCEL);
        if(pass){
            return Result.success("批量取消成功");
        } else {
            return Result.error("批量取消失败");
        }
    }


}